package com.leonzhangxf.spel.evaluation;

import lombok.Data;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;

/**
 * The SpEL compilation mechanism.
 *
 * @author leonzhangxf
 * @date 20190425
 */
public class SpElCompilation {

    public static void main(String[] args) {
        demo();
        System.out.println("=====================");
    }

    private static void demo() {
        SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,
            SpElCompilation.class.getClassLoader());

        SpelExpressionParser parser = new SpelExpressionParser(config);

        Expression expr = parser.parseExpression("payload");

        Message message = new Message();
        System.out.println(expr.getValue(message));
    }

    @Data
    static class Message {
        private String payload = "payload";
    }
}
